﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using YArchitech.HCLoad.Controls;
using YArchitech.HCLoad.Properties;
using YArchitech.HCLoad.Utils;

namespace YArchitech.HCLoad.Export
{
	public partial class ExportForm : System.Windows.Forms.Form
	{
		public ExportForm()
		{
			this.InitializeComponent();
		}

		public ExportForm(HcDataManager dataManager)
		{
			this.m_dataManager = dataManager;
			this.InitializeComponent();
		}

		private void OnLoad(object sender, EventArgs e)
		{
			HcRegistry.GetRegValue("FilterSettings", ref this.m_nFilterSettings);
			this.m_sTargetFileName = Path.ChangeExtension(this.m_dataManager.Document.PathName, "hcXml");
			HcRegistry.GetRegValue("ExportFileName", ref this.m_sTargetFileName);
			this.textBox_TargetFileName.Text = this.m_sTargetFileName;
			this.InitCheckboxs();
			this.InitCombobox();
			this.InitTextBox();
		}

		private void InitCheckboxs()
		{
			this.SetCheckBoxChecked(this.checkBox_Name, Convert.ToInt32(EnumFilterChecker.eName));
			this.checkBox_Name_CheckedChanged(null, null);
			this.SetCheckBoxChecked(this.checkBox_Area, Convert.ToInt32(EnumFilterChecker.eArea));
			this.checkBox_Area_CheckedChanged(null, null);
			this.SetCheckBoxChecked(this.checkBox_LoadItem_ExteriorWall, Convert.ToInt32(EnumFilterChecker.eExteriorWall));
			this.SetCheckBoxChecked(this.checkBox_LoadItem_InteriorWall, Convert.ToInt32(EnumFilterChecker.eInteriorWall));
			this.SetCheckBoxChecked(this.checkBox_LoadItem_Opening, Convert.ToInt32(EnumFilterChecker.eOpening));
			this.SetCheckBoxChecked(this.checkBox_LoadItem_Roof, Convert.ToInt32(EnumFilterChecker.eRoof));
			this.SetCheckBoxChecked(this.checkBox_LoadItem_Ceiling, Convert.ToInt32(EnumFilterChecker.eCeiling));
			this.SetCheckBoxChecked(this.checkBox_LoadItem_Floor, Convert.ToInt32(EnumFilterChecker.eFloor));
			this.SetCheckBoxChecked(this.checkBox_LoadItem_InteriorFloor, Convert.ToInt32(EnumFilterChecker.eInteriorFloor));
		}

		private void SaveFilterInfo()
		{
			this.m_nFilterSettings = 0;
			this.SaveCheckBoxChecked(this.checkBox_Name, Convert.ToInt32(EnumFilterChecker.eName));
			this.SaveCheckBoxChecked(this.checkBox_Area, Convert.ToInt32(EnumFilterChecker.eArea));
			this.SaveCheckBoxChecked(this.checkBox_LoadItem_ExteriorWall, Convert.ToInt32(EnumFilterChecker.eExteriorWall));
			this.SaveCheckBoxChecked(this.checkBox_LoadItem_InteriorWall, Convert.ToInt32(EnumFilterChecker.eInteriorWall));
			this.SaveCheckBoxChecked(this.checkBox_LoadItem_Opening, Convert.ToInt32(EnumFilterChecker.eOpening));
			this.SaveCheckBoxChecked(this.checkBox_LoadItem_Roof, Convert.ToInt32(EnumFilterChecker.eRoof));
			this.SaveCheckBoxChecked(this.checkBox_LoadItem_Ceiling, Convert.ToInt32(EnumFilterChecker.eCeiling));
			this.SaveCheckBoxChecked(this.checkBox_LoadItem_Floor, Convert.ToInt32(EnumFilterChecker.eFloor));
			this.SaveCheckBoxChecked(this.checkBox_LoadItem_InteriorFloor, Convert.ToInt32(EnumFilterChecker.eInteriorFloor));
			HcRegistry.SetRegValue("FilterSettings", this.m_nFilterSettings);
			HcRegistry.SetRegValue("NameFilterType", this.comboBox_Name.SelectedIndex);
			HcRegistry.SetRegValue("AreaRangeFilterType", this.comboBox_Area.SelectedIndex);
			HcRegistry.SetRegValue("NameFilterStr", this.textBox_Name.Text);
			HcRegistry.SetRegValue("MinSpaceArea", this.textBox_Area_min.Value);
			HcRegistry.SetRegValue("MaxSpaceArea", this.textBox_Area_max.Value);
		}

		private void SetCheckBoxChecked(CheckBox checkBox, int nBit)
		{
			int num = 1 << nBit;
			checkBox.Checked = (num == (this.m_nFilterSettings & num));
		}

		private void SaveCheckBoxChecked(CheckBox checkBox, int nBit)
		{
			if (checkBox.Checked)
			{
				int num = 1 << nBit;
				this.m_nFilterSettings |= num;
			}
		}

		private void InitCombobox()
		{
			int selectedIndex = Convert.ToInt32(StringFilterType.eInclude);
			HcRegistry.GetRegValue("NameFilterType", ref selectedIndex);
			this.comboBox_Name.SelectedIndex = selectedIndex;
			int selectedIndex2 = Convert.ToInt32(RangeFilterType.eGreaterThan);
			HcRegistry.GetRegValue("AreaRangeFilterType", ref selectedIndex2);
			this.comboBox_Area.SelectedIndex = selectedIndex2;
		}

		private void InitTextBox()
		{
			string text = "Space";
			HcRegistry.GetRegValue("NameFilterStr", ref text);
			this.textBox_Name.Text = text;
			double value = 3.0;
			double value2 = 300.0;
			HcRegistry.GetRegValue("MinSpaceArea", ref value);
			HcRegistry.GetRegValue("MaxSpaceArea", ref value2);
			this.textBox_Area_min.Value = value;
			this.textBox_Area_max.Value = value2;
		}

		private void button_TargetFile_Click(object sender, EventArgs e)
		{
			string text = this.textBox_TargetFileName.Text;
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.InitialDirectory = Path.GetDirectoryName(text);
			saveFileDialog.Filter = "hcXml files (*.hcXml)|*.hcXml|All files (*.*)|*.*";
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.FileName = Path.GetFileNameWithoutExtension(text);
			saveFileDialog.RestoreDirectory = true;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.m_sTargetFileName = saveFileDialog.FileName;
				this.textBox_TargetFileName.Clear();
				this.textBox_TargetFileName.Paste(this.m_sTargetFileName);
			}
		}

		private void button_cancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void ExportForm_Resize(object sender, EventArgs e)
		{
			if (base.WindowState == FormWindowState.Minimized)
			{
				base.Visible = false;
			}
		}

		private void ExportForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!base.Visible)
			{
				base.Visible = true;
			}
		}

		private void button_LevelInfo_Click(object sender, EventArgs e)
		{
			new LevelInfoForm(this.m_dataManager).ShowDialog();
		}

		private void checkBox_Name_CheckedChanged(object sender, EventArgs e)
		{
			bool @checked = this.checkBox_Name.Checked;
			this.comboBox_Name.Enabled = @checked;
			this.textBox_Name.Enabled = @checked;
		}

		private void checkBox_Area_CheckedChanged(object sender, EventArgs e)
		{
			bool @checked = this.checkBox_Area.Checked;
			this.comboBox_Area.Enabled = @checked;
			this.comboBox_Area_SelectedIndexChanged(sender, e);
		}

		private void checkBox_Number_CheckedChanged(object sender, EventArgs e)
		{
		}

		private void comboBox_Area_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.comboBox_Area.Enabled)
			{
				this.textBox_Area_min.Enabled = false;
				this.textBox_Area_max.Enabled = false;
				return;
			}
			switch (HcConvert.ToRangeFilterType(this.comboBox_Area.SelectedIndex))
			{
			case RangeFilterType.eGreaterThan:
				this.textBox_Area_min.Enabled = true;
				this.textBox_Area_max.Enabled = false;
				return;
			case RangeFilterType.eLessThan:
				this.textBox_Area_min.Enabled = false;
				this.textBox_Area_max.Enabled = true;
				return;
			case RangeFilterType.eInRange:
				this.textBox_Area_min.Enabled = true;
				this.textBox_Area_max.Enabled = true;
				return;
			default:
				return;
			}
		}

		private void textBox_Area_TextChanged(object sender, EventArgs e)
		{
		}

		private void button_Exoport_Click(object sender, EventArgs e)
		{
			this.SaveFilterInfo();
			this.m_sTargetFileName = this.textBox_TargetFileName.Text;
			if (this.m_sTargetFileName.Length == 0)
			{
				MessageBox.Show(Resources.Error_TargetFileName_Null);
				this.textBox_TargetFileName.Focus();
				return;
			}
			HcSpaceDataFilter hcSpaceDataFilter = new HcSpaceDataFilter();
			int num = (1 << 1 + Convert.ToInt32(EnumFilterChecker.eNumber)) - 1;
			hcSpaceDataFilter.FilterOptions = (this.m_nFilterSettings & num);
			hcSpaceDataFilter.NameFilterType = HcConvert.ToStringFilterType(this.comboBox_Name.SelectedIndex);
			hcSpaceDataFilter.Name = this.textBox_Name.Text;
			hcSpaceDataFilter.AreaFilterType = HcConvert.ToRangeFilterType(this.comboBox_Area.SelectedIndex);
			double v = HcUnit.AreaConverToAPI(this.m_dataManager.Document, this.textBox_Area_min.Value);
			double v2 = HcUnit.AreaConverToAPI(this.m_dataManager.Document, this.textBox_Area_max.Value);
			hcSpaceDataFilter.AreaRange = new HcValueRange(v, v2, hcSpaceDataFilter.AreaFilterType);
			this.m_dataManager.SetSpaceFilter(hcSpaceDataFilter);
			HcRegistry.SetRegValue("ExportFileName", this.m_sTargetFileName);
			if (this.m_dataManager.Export(this.m_sTargetFileName))
			{
				MessageBox.Show(Resources.MSG_Export_Succeed, Resources.MSGBOX_TITLE);
			}
			MessageBox.Show(this.m_dataManager.HcSpaces.Count.ToString());
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private const string sFileExternsion = "hcXml";

		private static readonly int nFilterCount = Convert.ToInt32(EnumFilterChecker.eFilterCount);

		private int m_nFilterSettings = (1 << ExportForm.nFilterCount) - 1;

		private string m_sTargetFileName;

		private HcDataManager m_dataManager;
	}
}
